/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package s.f.login;

import s.f.usuarios.CRUD_Usuarios;
import s.f.util.AppException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Froy
 */
public class Login extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        boolean valLogin = false;
        if (request.getParameter("BTNLOGIN") != null) {
            if (request.getParameter("BTNLOGIN").equals("Acceder")) {
                
                String usr = request.getParameter("TXTUSER");
                String pass = request.getParameter("TXTCONTRASENA");
                
                try {
                    valLogin = CRUD_Usuarios.compareUser(usr, pass);
                } catch (SQLException ex) {
                    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
                } catch (AppException ex) {
                    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                
                if (valLogin) {
                    HttpSession sesion = request.getSession(true);
                    sesion.setAttribute("usuario", request.getParameter("TXTUSER"));
                }
            }
        }
    }
}
